package org.hgvc.webapp.ets.acegi.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.apache.log4j.Logger;
import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Member;
import org.hgvc.webapp.ets.bo.Partner;
import org.hgvc.webapp.ets.constant.AgentConstant;
import org.hgvc.webapp.ets.constant.MemberConstant;
import org.hgvc.webapp.ets.constant.PartnerConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.Navigator;
import org.jocsb.base.bo.User;


public class NavigatorServiceImpl implements Navigator {
	
	private PartnerService partnerService;
	private AgentService agentService;
	private MemberService memberService;
	private static final Logger log = Logger.getLogger(NavigatorServiceImpl.class);
	
	
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public void setAgentService(AgentService agentService) {
		this.agentService = agentService;
	}
	public void setPartnerService(PartnerService partnerService) {
		this.partnerService = partnerService;
	}
	public String getTargetUrl(HttpServletRequest request,
			HttpServletResponse response, Authentication auth) {
		GrantedAuthority[] auths = auth.getAuthorities();
		for(GrantedAuthority g:auths){
			log.debug("Verify Authority = "+g.getAuthority().toString());
			if(g.getAuthority().equalsIgnoreCase(SystemConstant.PARTNER_TEMP_ROLE.getName())
				||g.getAuthority().equalsIgnoreCase(SystemConstant.PARTNER_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				return partnerNavUrl(auth);
			}
			if(g.getAuthority().equalsIgnoreCase(SystemConstant.AGENT_TEMP_ROLE.getName())
					||g.getAuthority().equalsIgnoreCase(SystemConstant.AGENT_ROLE.getName())
					||g.getAuthority().equalsIgnoreCase(SystemConstant.AGENT_SUB_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				return agentNavUrl(auth);
			}if(g.getAuthority().equalsIgnoreCase(SystemConstant.MEMBER_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				return memberNavUrl(auth);
			}
			if(g.getAuthority().equalsIgnoreCase(SystemConstant.ADMIN_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				log.debug("Redirecting to Admin User Portal");
				return "/secure/portal/admin/admin.portal.jsf";
			}
			if(g.getAuthority().equalsIgnoreCase(SystemConstant.FINANCE_ADMIN_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				log.debug("Redirecting to Finance User Portal");
				return "/secure/portal/finance/finance.portal.jsf";
			}
			if(g.getAuthority().equalsIgnoreCase(SystemConstant.SUPER_ADMIN_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				log.debug("Redirecting to SuperAdmin User Portal");
				return "/secure/portal/superAdmin/portal.jsf";
			}
			if(g.getAuthority().equalsIgnoreCase(SystemConstant.SALES_ADMIN_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				log.debug("Redirecting to SalesAdmin User Portal");
				return "/secure/portal/salesadmin_role/portal.jsf";
			}
			if(g.getAuthority().equalsIgnoreCase(SystemConstant.MEMBER_SERVICE_ROLE.getName())){
				log.debug("Valid Authority = "+g.getAuthority().toString());
				log.debug("Redirecting to Member Service User Portal");
				return "/secure/portal/memberservice_role/portal.jsf";
			}
		}

		return "";
	}
	
	public String memberNavUrl(Authentication auth){
		try{
			User user = (User)auth.getPrincipal();
			Member member = memberService.getMemberByUserId(user.getUserId());
			
			if(!member.getStatus().equalsIgnoreCase(MemberConstant.MEMBER_STATUS_MEMBER)){
				log.debug("Redirecting to Member Registration Pending Acknowledgement");
				return "/public/register/member.registration.pending.ack.jsf"; 
			}else{
				log.debug("Redirecting to Member Portal");
				return "/secure/portal/member/member.portal.jsf";
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public String agentNavUrl(Authentication auth){
		try{
			User user = (User)auth.getPrincipal();
			Agent agent = agentService.getAgentByUserId(user.getUserId());
			
			if(agent.getStatus().equalsIgnoreCase(AgentConstant.AGENT_STATUS_PENDING_APPROVAL_REG)){
				log.debug("Redirecting to Agent Registration Pending Acknowledgement");
				return "/public/agent/agent.registration.pending.ack.jsf";
			}
			else if(agent.getStatus().equalsIgnoreCase(AgentConstant.AGENT_STATUS_REJECT_REG)){
				log.debug("Redirecting to Rejected Agent Registration Acknowledgement");
				return "/public/agent/agent.registration.reject.ack.jsf";
			}
			else if(agent.getStatus().equalsIgnoreCase(AgentConstant.AGENT_STATUS_APPROVE_AGENT)){
				log.debug("Redirecting to Agent Portal");
				return "/secure/portal/agent/agent.portal.jsf";
			}else if(agent.getStatus().equalsIgnoreCase(AgentConstant.AGENT_STATUS_SUB_AGENT)){
				log.debug("Redirecting to Agent Portal");
				return "/secure/portal/agent/agent.portal.jsf";
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	
	}
	
	public String partnerNavUrl(Authentication auth){
		try{
			User user = (User)auth.getPrincipal();
			Partner partner = partnerService.getPartnerByUserId(user.getUserId());
			
			if(partner.getStatus().equalsIgnoreCase(PartnerConstant.PARTNER_STATUS_PENDING_APPROVAL_REG)){
				log.debug("Redirecting to Partner Registration Pending Acknowledgement");
				return "/public/partner/partner.registration.pending.ack.jsf";
			}
			else if(partner.getStatus().equalsIgnoreCase(PartnerConstant.PARTNER_STATUS_REJECT_REG)){
				log.debug("Redirecting to Rejected Partner Registration Acknowledgement");
				return "/public/partner/partner.registration.reject.ack.jsf";
			}
			else if(partner.getStatus().equalsIgnoreCase(PartnerConstant.PARTNER_STATUS_PENDING_CHECK_LIST)){
				log.debug("Redirecting to Pending Partner Document Submission Acknowledgement");
				return "/public/partner/partner.doc.submission.pending.ack.jsf";
			}
			else if(partner.getStatus().equalsIgnoreCase(PartnerConstant.PARTNER_STATUS_APPROVE_PARTNER)){
				log.debug("Redirecting to Partner Portal");
				return "/secure/portal/partner/partner.portal.jsf";
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	
	}

}
